// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

const db = cloud.database()
const _ = db.command
var today = new Date().toLocaleDateString() //获取当前时间把Date对象的日期部分转换为字符串"2020/8/4"
var threetime = (new Date).getTime() - 24 * 60 * 60 * 1000 * 2
var threeTime = new Date(threetime).toLocaleDateString()

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  return await db.collection("song")
    .where({
      //date是字段，数据类型是date对象，_.gte和_.lte这里要传入date对象，
      //使用例如2020-08-04 12:13:29格式传入
      time: _.and(_.gte(new Date(threeTime + " 00:00:00")), _.lte(new Date(today + " 23:59:59"))),
      _openid: wxContext.OPENID
    })
    .count()
}